package com.hzjq.fxlib.presenter

import android.app.Activity
import android.content.Context
import android.view.KeyEvent
import android.view.View
import android.view.ViewGroup

abstract class AbsViewPresenter {

    internal var rootView: ViewGroup
    internal var childView: View

    constructor(rootView: ViewGroup) {
        this.rootView = rootView
        this.childView = View.inflate(getContext(), onContentViewId(), null)
        rootView.addView(childView,ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT))
        childView.visibility = View.GONE
    }

    fun getContext(): Context {
        return rootView.context
    }

    fun finish(){
        (getContext() as Activity).finish()
    }

    fun show() {
        for(i in 0 until rootView.childCount){
            val childAt = rootView.getChildAt(i)
            childAt.visibility =if(childAt == childView) View.VISIBLE else View.GONE
            if(childAt == childView){
                onResume()
            }
        }
    }

    open fun hide(){}

    abstract fun onContentViewId(): Int


    abstract fun onResume()

    abstract fun showAction(action: String)

    open fun notifyProgressBar(action: String, p: Int) {}

    open fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        return false
    }

}